<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>采购单</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/add.css">

    <link rel="stylesheet" href="./css/common.css">
</head>

<body>
    <header>
        <div class="wel" style="margin-top:10px;">
            <h3>欢迎来到强哥图书管理系统</h3>
        </div>
        <nav>
            <ul id="nav-list" style=" margin-top:19px;"></ul>
        </nav>
    </header>

    <div class="container">

        <div class="form-inline">
            <h2 style="text-align: left; margin-left: 10px;"><svg xmlns="http://www.w3.org/2000/svg" width="40"
                    fill="#17a2b8" class="bi bi-book-half" viewBox="0 0 16 16">
                    <path
                        d="M8.5 2.687c.654-.689 1.782-.886 3.112-.752 1.234.124 2.503.523 3.388.893v9.923c-.918-.35-2.107-.692-3.287-.81-1.094-.111-2.278-.039-3.213.492V2.687zM8 1.783C7.015.936 5.587.81 4.287.94c-1.514.153-3.042.672-3.994 1.105A.5.5 0 0 0 0 2.5v11a.5.5 0 0 0 .707.455c.882-.4 2.303-.881 3.68-1.02 1.409-.142 2.59.087 3.223.877a.5.5 0 0 0 .78 0c.633-.79 1.814-1.019 3.222-.877 1.378.139 2.8.62 3.681 1.02A.5.5 0 0 0 16 13.5v-11a.5.5 0 0 0-.293-.455c-.952-.433-2.48-.952-3.994-1.105C10.413.809 8.985.936 8 1.783z" />
                </svg>
                <span>采购单</span>
            </h2>
        </div>

        <form id="addBook">
            <div class="form-group">
                <label for="buyerName">采购员</label>
                <input type="text" class="form-control" placeholder="请输入采购员姓名" id="buyerName" name="buyerName">
            </div>
            <div class="form-group">
                <label for="buyerPhone">联系方式</label>
                <input type="text" class="form-control" placeholder="请输入采购员联系方式" id="buyerPhone" name="buyerPhone" />
            </div>
            <div class="form-group">
                <label for="merchant">商家</label>
                <input type="text" class="form-control" placeholder="请输入商家单位" id="merchant" name="merchant" />
            </div>

            <div class="form-group">
                <label for="merchantPhone">联系方式</label>
                <input type="text" class="form-control" placeholder="请输入商家的联系方式" id="merchantPhone" name="merchantPhone">
            </div>

            <div class="form-group">
                <label for="totalPrice">交易金额</label>
                <input type="number" id="totalPrice" class="form-control" placeholder="请输入交易金额" name="totalPrice" />
            </div>
            <div class="form-group">
                <label for="isPaid">是否支付</label>
                <select class="custom-select" id="isPaid" name="isPaid">
                    <option value="1" selected>已支付</option>
                    <option value="2">未支付</option>
                </select>
            </div>

            <div class="form-group">
                <label for="spending">支出金额</label>
                <input type="number" id="spending" class="form-control" placeholder="请输入支出金额" name="spending" />
            </div>
            <div class="form-group">
                <label for="isSettleAccount">是否清账</label>
                <select class="custom-select" id="isSettleAccount" name="isSettleAccount">
                    <option value="1" selected>已结清</option>
                    <option value="2">未结清</option>
                </select>
            </div>

            <!-- <a href="javascript:void(0);" onclick="window.location.href='addbook_list.html?buyerName=' + encodeURIComponent(buyerName);">查看进货图书清单</a> -->

        </form>
        <div class="form-group" style="text-align: right">
            <button type="button" class="btn btn-info btn-lg" onclick="add()">确定</button>
            <a href="buyBook_detail.html">去添购图书</a>
        </div>

        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script>
const storedData = JSON.parse(localStorage.getItem('loginData')) || {};
console.log("storedData",storedData);
        const userRole = storedData || 'guest'; // 假设用户数据中有一个'role'字段
 console.log("userRole",userRole);
        // 获取导航列表的DOM元素
        const navList = document.getElementById('nav-list');
 
        // 创建一个函数来添加导航项
        function addNavItem(text, href) {
            const li = document.createElement('li');
            const a = document.createElement('a');
            a.href = href;
            a.textContent = text;
            li.appendChild(a);
            navList.appendChild(li);
        }
 
        // 根据用户角色添加相应的导航项
        if (userRole === 'admin') {
            addNavItem('登录', 'login.html');
            addNavItem('注册', 'register.html');
            addNavItem('图书列表', 'book_list.html');
            addNavItem('归还图书', 'return_list.html');
            addNavItem('采购界面', 'purchase.html');
            addNavItem('采购清单', 'addbook_list.html');
            addNavItem('个人中心', 'personal_admin.html');
            // addNavItem('c', 'book_list.html');
            // addNavItem('图书列表', 'book_list.html');
            // ... 其他管理员可见的链接
        } else if (userRole === 'user') {
            addNavItem('登录', 'login.html');
            addNavItem('注册', 'register.html');
            addNavItem('图书列表', 'book_list_B.html');       
            addNavItem('个人中心', 'personal.html');
            // ... 其他用户可见的链接
        } else {
            // 对于未登录或未知角色的用户，可能只显示登录链接
            addNavItem('登录', 'login.html');

        }

            function add() {
                $.ajax({
                    url: "/buy/insertPurchase",
                    type: "post",
                    data: $("#addBook").serialize(),
                    success: function (result) {
                        console.log("1" + result);
                        if (result) {
                            // if(result != null && result.data == true) {
                            alert("添加成功");

                        } else {
                            alert("添加失败，请检查图书信息");
                        }
                    },
                    error: function (error) {
                        if (error.status == 401) {
                            location.href = "login.html";
                        }
                    }
                });
            }
        </script>
    </div>
</body>

</html>